//
//  CalendarViewController.m
//  LTSCalendar
//
//  Created by LTS on 2018/1/12.
//  Copyright © 2018年 LTS. All rights reserved.
//

#import "CalendarViewController.h"
#import "LTSCalendarManager.h"

@interface CalendarViewController ()<LTSCalendarEventSource>
{
    NSMutableDictionary *eventsByDate;
}



@property (nonatomic,strong)LTSCalendarManager *manager;

@end

@implementation CalendarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"日程管理";
    
    [LTSCalendarAppearance share].isShowLunarCalender = YES;
    
    self.manager = [LTSCalendarManager new];
    self.manager.eventSource = self;
    
    // WeekDay View
    self.manager.weekDayView = [[LTSCalendarWeekDayView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    [self.view addSubview:self.manager.weekDayView];
    
    // Calender View
    self.manager.calenderScrollView = [[LTSCalendarScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.manager.weekDayView.frame), SCREEN_WIDTH, CGRectGetHeight(self.view.frame)-CGRectGetMaxY(self.manager.weekDayView.frame))];
    [self.view addSubview:self.manager.calenderScrollView];
    
    self.automaticallyAdjustsScrollViewInsets = false;
    
    // 添加事件
    [self createRandomEvents];
    
    // 默认选中当天
    [self calendarDidSelectedDate:[NSDate date]];
}


- (void)calendarDidLoadPageCurrentDate:(NSDate *)date {
    NSLog(@"%@", [NSString stringWithFormat:@"%@",date]);
}

// 点击 日期后的执行的操作
- (void)calendarDidSelectedDate:(NSDate *)date {
    NSLog(@"%@", [NSString stringWithFormat:@"%@",date]);
    
    NSString *key = [[NSDate dateFormatter:@"yyyy-MM-dd"] stringFromDate:date];
    NSArray *events = eventsByDate[key];
    
    // 添加数据
    if (events.count > 0) {
        
        // 数据去重
        NSSet *set = [NSSet setWithArray:events];
        
        self.manager.calenderScrollView.datasArray = [set allObjects];
        
    }else {
        
        self.manager.calenderScrollView.datasArray = @[];
    }
    
    [self.manager.calenderScrollView.tableView reloadData];
    
}

// 该日期是否有事件
- (BOOL)calendarHaveEventWithDate:(NSDate *)date {
    
    NSString *key = [[NSDate dateFormatter] stringFromDate:date];
    
    if(eventsByDate[key] && [eventsByDate[key] count] > 0){
        return YES;
    }
    return NO;
}

// 添加事件
- (void)createRandomEvents
{
    eventsByDate = [NSMutableDictionary new];
    
    for(int i = 0; i < 30; ++i) {
        
        // Generate 30 random dates between now and 60 days later
        NSDate *randomDate = [NSDate dateWithTimeInterval:(rand() % (3600 * 24 * 60)) sinceDate:[NSDate date]];
        
        // Use the date as key for eventsByDate
        NSString *key = [[NSDate dateFormatter] stringFromDate:randomDate];
        
        if(!eventsByDate[key]){
            eventsByDate[key] = [NSMutableArray new];
        }
        
        [eventsByDate[key] addObject:randomDate];
    }
    
    [self.manager reloadAppearanceAndData];
}




@end
